package com.loooooo.pm2024.modules.sys.service;


import com.loooooo.pm2024.modules.sys.mapper.DictCatalogMapper;
import com.loooooo.pm2024.modules.sys.mapper.DictMapper;
import com.loooooo.pm2024.modules.sys.model.Dict;
import com.loooooo.pm2024.modules.sys.model.DictCatalog;
import com.loooooo.pm2024.modules.sys.selector.DictCatalogSelector;
import com.loooooo.pm2024.modules.sys.selector.DictSelector;
import com.loooooo.pm2024.security.SecurityUtils;
import com.loooooo.pm2024.service.IdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictService {

    @Autowired
    DictMapper dictMapper;
    @Autowired
    IdService idService;


    public List<Dict> list(DictSelector selector) {
        return dictMapper.select(selector);
    }

    public int count(DictSelector selector) {
        return dictMapper.count(selector);
    }

    public int update(Dict dictCatalog) {
        long now = System.currentTimeMillis();
        Long userId = SecurityUtils.loginedUserId();
        dictCatalog.setUpdatedAt(now);
        dictCatalog.setUpdatedBy(userId);

        if (dictCatalog.getId() == null || dictCatalog.isNewEntity() || dictCatalog.getId() <= 0L) {
            if (dictCatalog.getId() == null || dictCatalog.getId() <= 0L) {
                dictCatalog.setId(idService.nextId());
            }
            dictCatalog.setCreatedAt(now);
            dictCatalog.setCreatedBy(userId);
            dictMapper.insert(dictCatalog);
        }

        return dictMapper.update(dictCatalog);
    }
}
